package com.iretailer.report.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.modelmapper.ModelMapper;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.*;

/**
 * @Auther: iretailer_xjw
 * @Date: 2019/1/18 15:00
 * @Description:
 */
@Configuration
public class BeanConfig {


    //配置springMVC返回的json 是使用 fastjson
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        //下面是配置一些 fastjson信息 例如：SerializerFeature.WriteMapNullValue 让值 为null 的属性也在 json中显示
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,
                SerializerFeature.DisableCircularReferenceDetect,
                SerializerFeature.WriteMapNullValue);
        fastConverter.setFastJsonConfig(fastJsonConfig);
        HttpMessageConverter<?> converter = fastConverter;
        return new HttpMessageConverters(converter);
    }

    /**
     * @Author xjw
     * @Date 2019/1/18 15:11
     * @Description :  用于定时任务
     **/
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() {
        return new SchedulerFactoryBean();
    }

    /**
     * @Author xjw
     * @Date 2019/1/18 15:11
     * @Description :  用于 发起 http请求
     **/
    @Bean
    public RestTemplate restTemplate() {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(15000);
        requestFactory.setReadTimeout(15000);
        return new RestTemplate(requestFactory);
    }


    @Bean
    public ModelMapper modelMapper() {
        return new ModelMapper();
    }

    /**
     * 定时任务需要
     *
     * @return
     */
    @Bean
    public TaskScheduler schedulerExecutorService() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(10);
        scheduler.setThreadNamePrefix("scheduled-thread-");
        return scheduler;
    }

    /**
     * 消费队列线程
     *
     * @return
     */
    @Bean(value = "threadPool")
    public ExecutorService buildConsumerQueueThreadPool() {
        return Executors.newCachedThreadPool();
    }

}
